import apiClient from './apiClient'

export const authService = {
  // 用户登录 - 适配后端ApiResponse格式
  login: async (credentials) => {
    const response = await apiClient.post('/system/auth/login', credentials)
    // 后端返回格式: { success: boolean, code: number, message: string, data: object }
    if (response.success) {
      return response.data // 返回用户数据
    } else {
      throw new Error(response.message || '登录失败')
    }
  },

  // 用户注册 - 适配后端ApiResponse格式
  register: async (userData) => {
    const response = await apiClient.post('/system/auth/register', userData)
    if (response.success) {
      return response.data // 返回用户数据
    } else {
      throw new Error(response.message || '注册失败')
    }
  },

  // 获取用户信息 - 适配后端ApiResponse格式
  getCurrentUser: async () => {
    const response = await apiClient.get('/system/auth/profile')
    if (response.success) {
      return response.data // 返回用户信息
    } else {
      throw new Error(response.message || '获取用户信息失败')
    }
  },

  // 重置密码 - 适配后端ApiResponse格式
  resetPassword: async (email) => {
    const response = await apiClient.post('/system/auth/reset-password', { email })
    if (response.success) {
      return response.data // 返回重置结果
    } else {
      throw new Error(response.message || '重置密码失败')
    }
  },

  // 退出登录
  logout: () => {
    localStorage.removeItem('cms_token')
    localStorage.removeItem('cms_user')
  }
}